package com.vm.adapter;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import com.vm.activity.ActiveActivity;
import com.vm.adapter.GoodsTypeGridAdapter.ViewHolder;
import com.vm.commons.StringUtil;
import com.vm.commons.VMConst;
import com.vm.model.AppType;
import com.vm.model.Goods;
import com.vm.model.GoodsType;
import com.vm.utils.JsonUtils;
import com.wwt.app.R;

public class GoodsTypeGridAdapter extends BaseAdapter {

	private Context mContext;
	private List<GoodsType> list_goodsType;
	RequestQueue requestQueue;
	GridView goodsGridView;

	/**
	 *  goodsGridView
	 */
	public GoodsTypeGridAdapter(Context context,
			List<GoodsType> list_goodsType, GridView goodsGridView) {
		this.mContext = context;
		this.list_goodsType = list_goodsType;
		this.goodsGridView = goodsGridView;
		requestQueue = Volley.newRequestQueue(context);

	}

	@Override
	public int getCount() {
		return list_goodsType.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder viewHolder = null;
		if (convertView == null) {

			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.mall_category_item, null);
			viewHolder = new ViewHolder();

			viewHolder.btn_goodsType = (TextView) convertView
					.findViewById(R.id.btn_goodsType);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		final GoodsType goodsType = list_goodsType.get(position);

		viewHolder.btn_goodsType.setTag(goodsType.getId());
		viewHolder.btn_goodsType.setText(goodsType.getName());
		viewHolder.btn_goodsType.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(final View v) {
				// goodsType.getId();
				// Intent intent = new Intent(v.getContext(),
				// ActiveActivity.class);
				// intent.putExtra("activeType", "getSoftByType?typeId="
				// + goodsType.getId());
				// intent.putExtra("currentType", goodsType.getName());
				// v.getContext().startActivity(intent);

				String typeId = Integer.toString(goodsType.getId());
				final List<Goods> list_goods = new ArrayList<Goods>();
				String url2 = VMConst.REQUEST_URL + "getGoodsByType?typeId="
						+ typeId;
				JsonArrayRequest goodsJsonArrayRequest = new JsonArrayRequest(
						url2, new Listener<JSONArray>() {
							@Override
							public void onResponse(JSONArray arg0) {

								for (int i = 0; i < arg0.length(); i++) {
									try {
										JSONObject jsonObject = arg0
												.getJSONObject(i);
										Goods goods = JsonUtils.jsonToBean(
												jsonObject, Goods.class);
										list_goods.add(goods);

									} catch (JSONException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									}
								}

								GoodsGridAdapter myAdapter = new GoodsGridAdapter(
										mContext, list_goods);
								
								// gridview.setOnItemClickListener(gd_listener);
								goodsGridView.setAdapter(myAdapter);
								//
								// Log.i(TAG,
								// Integer.toString(listsofts.size()));
								// // arg0.getJSONObject(index);

							}

						}, new Response.ErrorListener() {
							@Override
							public void onErrorResponse(VolleyError error) {
								// Log.e(TAG, error.getMessage(), error);
							}
						});

				requestQueue.add(goodsJsonArrayRequest);

			}
		});
		// Log.i("TEST", (String) viewHolder.btn_small_category.getText());

		return convertView;

	}

	static class ViewHolder {
		TextView btn_goodsType;

	}

}
